



#import "FootballViewController.h"


#import "BasketballDetailViewController.h"





#import "ScoreViewController.h"

#import "FirstFolltballViewCell.h"
#import "FirstFootballModel.h"

//数据库存储关注
#import "MyBassketballPointTool.h"


#pragma mark - Score 主界面

#define kTitleY kScreenIp6(20)
#define kBtnWidth kScreenIp6(60)
#define kBtnHeight kScreenIp6(40)


//事件


@interface FootballViewController ()<UITableViewDelegate,UITableViewDataSource>

@property(nonatomic,strong)CommonView *scoreView;




@property(nonatomic,strong)NSMutableArray *matchInfoModelArray;

@property(nonatomic,strong)NSMutableArray * dateModelArray;

/**
 *表头日期数组
 */
@property(nonatomic,strong)NSMutableArray *dateSectionArray;




/**
 *篮球关注
 */
@property(nonatomic,strong)NSMutableArray *pointBassketballModelArray;




// 判定篮球是否被选中
@property(nonatomic,getter=isSelected) BOOL selected;

@end

@implementation FootballViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    
    self.scoreView = [[CommonView alloc] init];
    
    
    self.scoreView.baseScrollView.delegate = self;
    self.view = _scoreView;
    
    // 左边
    self.scoreView.leftTableView.delegate = self;
    self.scoreView.leftTableView.dataSource = self;
    
    
    // 右边
    self.scoreView.rightTableView.delegate = self;
    self.scoreView.rightTableView.dataSource = self;
    
    
    self.scoreView.leftTableView.rowHeight = kScreenIp6(90);
    
    self.scoreView.rightTableView.rowHeight = kScreenIp6(90);
    
    //标题
    [self.scoreView.importantBtn setTitle:@"全部" forState:UIControlStateNormal];
    [self.scoreView.totalBtn setTitle:@"关注" forState:UIControlStateNormal];
    
    
    [self JionJieXiForFirstFootball];
    
    
    //下拉刷新
    
    [self  NoEndPullDownToRefresh];
    
    //增加的
    //push 过来,左边应该是 篮球  ,右边是足球
    [self.scoreView.leftButton setTitle:@"篮球" forState:UIControlStateNormal];
    [self.scoreView.rightButton setTitle:@"足球" forState:UIControlStateNormal];
    
    
    
    
    [self.scoreView.leftButton addTarget:self action:@selector(leftButton:) forControlEvents:UIControlEventTouchUpInside];
    [self.scoreView.rightButton addTarget:self action:@selector(rightButton:) forControlEvents:UIControlEventTouchUpInside];
    [self.scoreView.nextButton addTarget:self action:@selector(nextButton:) forControlEvents:UIControlEventTouchUpInside];
    [self.scoreView.importantBtn addTarget:self action:@selector(recommendBtnAction) forControlEvents:UIControlEventTouchUpInside];
    [self.scoreView.totalBtn addTarget:self action:@selector(videoBtnAction) forControlEvents:UIControlEventTouchUpInside];
    
    
    
    
    self.selected = NO;
    
    
    
    
    // 添加空白的 footView , 防止界面显示不全
    UIView *footerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenIp6(5))];
    self.scoreView.rightTableView.tableFooterView = footerView;
    self.scoreView.leftTableView.tableFooterView = footerView;
    
    self.scoreView.leftTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    
    self.scoreView.rightTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    
    
}
#pragma mark  关注的数组
-(NSMutableArray *)pointBassketballModelArray
{
    if(_pointBassketballModelArray ==nil)
    {
        self.pointBassketballModelArray = [NSMutableArray array];
    }
    
    return  _pointBassketballModelArray;
    
    
}

#pragma mark  下拉刷新


//刷新未结束的
- (void)NoEndPullDownToRefresh
{
    __unsafe_unretained typeof(self) vc = self;
    [self.scoreView.leftTableView  addLegendHeaderWithRefreshingBlock:^{
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            // 刷新表格
            [vc  JionJieXiForFirstFootball];
            // (最好在刷新表格后调用)调用endRefreshing可以结束刷新状态
            [vc.scoreView.leftTableView.header endRefreshing];
            [vc.scoreView.leftTableView  reloadData];
        });
    }];
}







#pragma mark  解析篮球---
-(void)JionJieXiForFirstFootball
{
    
    NSString *urlString = @"http://api.caipiao.163.com/live_jclq.html?mobileType=android&ver=4.8&channel=wap&apiVer=1.1&apiLevel=16";
    self.dateModelArray = [NSMutableArray array];
    
    self.dateSectionArray = [NSMutableArray array];
    
    [LORequestManger GET:urlString success:^(id response)
     {
         NSDictionary *bigDict =( NSDictionary*)response;
         
         NSArray*dataArray = [bigDict objectForKey:@"data"];
         
         
         for(long int i = dataArray.count-1; i>=0;i--)
         {
             NSDictionary *superDict = dataArray[i];
             
             [self.dateSectionArray addObject:superDict[@"period"] ];
             
             
             NSArray *matchInfoArray = [superDict objectForKey:@"matchInfo"];
             
             self.matchInfoModelArray =[NSMutableArray array];
             
             
             
             for( long int j= matchInfoArray.count -1; j>=0; j--)
             {
                 FirstFootballModel *model = [FirstFootballModel FirstFootballModelWithDict:matchInfoArray[j]];
                 [self.matchInfoModelArray addObject:model];
                 
                 
             }
             
             
             [self.dateModelArray addObject:_matchInfoModelArray];
         }
         
#warning 数据为空时, 显示图片
         if (self.dateModelArray.count == 0)
         {
             
             UIView *noDataView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight)];
             noDataView.backgroundColor = [UIColor whiteColor];
             
             
             
             UIImageView *pointPicture = [[UIImageView alloc]initWithFrame:CGRectMake(kScreenWidth/2 - kScreenIp6(100), kScreenIp6(100), kScreenIp6(200), kScreenIp6(200))];
             pointPicture.image = [UIImage imageNamed:@"Empty_Hint_ZZZ@2x"];
             
             
             [noDataView addSubview:pointPicture];

             [self.scoreView.baseScrollView  addSubview:noDataView];
         }
         
         
         [self.scoreView.leftTableView reloadData];
     } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
         NSLog(@"%@",error);
         NSLog(@"网络错误");
     }];
    
    
}



#pragma mark - 代理方法


#pragma mark - 自定义分区头视图
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    if(tableView ==self.scoreView.leftTableView)
    {
        SectionHeaderView *headerView = [[SectionHeaderView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenIp6(30))];
        
        
        headerView.headerLabel.text = self.dateSectionArray[section];
        
        return headerView;
    }
    else
    {
        
        SectionHeaderView *headerView = [[SectionHeaderView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenIp6(0))];
        
        
        headerView.headerLabel.text = @"123";
        
        
        return headerView;
        
        
        
    }
    
}



- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    
    
    if (tableView ==self.scoreView.leftTableView)
    {
        return kScreenIp6(30);
    }
    
    else
    {
        return kScreenIp6(0);
    }
}




- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    
    if(tableView ==self.scoreView.leftTableView)
    {
        
        return self.dateModelArray.count;
        
    }
    else
    {
        return 1;
    }
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if(tableView ==self.scoreView.leftTableView)
    {
        
        NSArray *tempArray =[self.dateModelArray objectAtIndex:section];
        
        
        return tempArray.count;
    }
    
    else
    {
        
        return  self.pointBassketballModelArray.count;
    }
    
    
    
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    //全部
    if(tableView ==self.scoreView.leftTableView)
    {
        static NSString *cellIndetifier = @"ScoreCell";
        [tableView registerClass:[FirstFolltballViewCell class] forCellReuseIdentifier:cellIndetifier];
        
        FirstFolltballViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIndetifier forIndexPath:indexPath];
        
        FirstFootballModel*model = self.dateModelArray[indexPath.section][indexPath.row];
        
        model.isSelectPoint = NO;
        
        [cell setCellValueWithModel:model];
        
        
        
        //
        [cell.attentionIcon  addTarget:self action:@selector(pointAcion:) forControlEvents:UIControlEventTouchUpInside];
        
        
        for(FirstFootballModel *modelPoint  in self.pointBassketballModelArray)
            
        {
            
            if([model.matchId isEqualToString:modelPoint.matchId])
            {
                model.isSelectPoint =YES;
                
                
            }
            
            
        }
        
        
        
        if(model.isSelectPoint)
        {
            
            [cell.attentionIcon  setBackgroundImage:[UIImage imageNamed:@"football_collected_small_1@2x"] forState:UIControlStateNormal];
            
        }
        else
        {
            
            
            [cell.attentionIcon setBackgroundImage:[UIImage imageNamed:@"football_collect_small_1"] forState:UIControlStateNormal];
            
            
        }
        
        
        
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        
        return cell;
        
    }
    //关注
    else
    {
        
        
        static NSString *cellIndetifier = @"pointCell";
        [tableView registerClass:[FirstFolltballViewCell class] forCellReuseIdentifier:cellIndetifier];
        
        FirstFolltballViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIndetifier forIndexPath:indexPath];
        
        FirstFootballModel*model = self.pointBassketballModelArray[indexPath.row];
        
        
        
        [cell.attentionIcon  addTarget:self action:@selector(pointCancelAcion:) forControlEvents:UIControlEventTouchUpInside];
        
        
        if(model.isSelectPoint)
        {
            
            [cell.attentionIcon  setBackgroundImage:[UIImage imageNamed:@"football_collected_small_1@2x"] forState:UIControlStateNormal];
            
        }
        
        [cell setCellValueWithModel:model];
        
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        
        return cell;
        
        
    }
    
    
}


//#pragma mark  点击cell
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    // 点击 cell 之前先判断 rightButton 是否是隐藏的
    if (self.scoreView.rightButton.hidden)
    {
        
        if(tableView ==self.scoreView.leftTableView)
        {
            
            FirstFootballModel *model = self.dateModelArray[indexPath.section][indexPath.row];
            
            
            BasketballDetailViewController *detailMessageVC= [[BasketballDetailViewController alloc]init];
            
            
            detailMessageVC.detailModel = model;
            
            
            
            [self.navigationController pushViewController:detailMessageVC animated:YES];
            
        }
        else
        {
            
            FirstFootballModel *model = self.pointBassketballModelArray[indexPath.row];
            
            BasketballDetailViewController *detailMessageVC= [[BasketballDetailViewController alloc]init];
            
            
            
            detailMessageVC.detailModel = model;
            
            
            
            [self.navigationController pushViewController:detailMessageVC animated:YES];
            
            
        }
        
    }
    else
    {
        [self leftButton:self.scoreView.leftButton];
    }
    
}






#pragma mark - 点击导航栏右边按钮
- (void)rightButton:(UIButton *)sender
{
    [UIView beginAnimations:nil context:nil];
    [UIView setAnimationDuration:1];
    [UIView setAnimationDelegate:self];
    
    self.scoreView.rightButton.hidden = YES;
    self.scoreView.nextButton.hidden = NO;
    NSString *string = self.scoreView.leftButton.titleLabel.text;
    [self.scoreView.leftButton setTitle:self.scoreView.rightButton.titleLabel.text forState:UIControlStateNormal];
    [self.scoreView.rightButton setTitle:string forState:UIControlStateNormal];
    self.scoreView.leftButton.frame = CGRectMake((kScreenWidth - kBtnWidth- kScreenIp6(10)) / 2, kTitleY, kBtnWidth, kBtnHeight);
    
    [UIView commitAnimations];
    self.selected = !self.selected;
    [self.scoreView.leftTableView reloadData];
    
    
    [self.navigationController popViewControllerAnimated:NO];
}


//足球
- (void)leftButton:(UIButton *)sender
{
    [UIView beginAnimations:nil context:nil];
    [UIView setAnimationDuration:1];
    [UIView setAnimationDelegate:self];
    
    if (self.scoreView.rightButton.hidden)
    {
        self.scoreView.rightButton.hidden = NO;
        self.scoreView.nextButton.hidden = YES;
        self.scoreView.leftButton.frame = CGRectMake(kScreenWidth / 2 - kBtnWidth - kScreenIp6(10), kTitleY, kBtnWidth, kBtnHeight);
    }
    else
    {
        self.scoreView.rightButton.hidden = YES;
        self.scoreView.nextButton.hidden = NO;
        self.scoreView.leftButton.frame = CGRectMake((kScreenWidth - kBtnWidth- kScreenIp6(10)) / 2, kTitleY, kBtnWidth, kBtnHeight);
    }
    [UIView commitAnimations];
    
    
    
    
}

- (void)nextButton:(UIButton *)sender
{
    [self leftButton:self.scoreView.leftButton];
}




#pragma mark - 重要和全部的点击事件
//全部
- (void)recommendBtnAction
{
    if (self.scoreView.rightButton.hidden) {
        [UIView beginAnimations:nil context:nil];
        [UIView setAnimationDuration:0.5];
        [UIView setAnimationDelegate:self];
        
        
        self.scoreView.baseScrollView.contentOffset = CGPointMake(0, 0);
        [self switchButtonPoint];
        
        
        [UIView commitAnimations];
    }
    else
    {
        [self leftButton:self.scoreView.leftButton];
    }
}


//关注
- (void)videoBtnAction
{
    if (self.scoreView.rightButton.hidden) {
        [UIView beginAnimations:nil context:nil];
        [UIView setAnimationDuration:0.5];
        [UIView setAnimationDelegate:self];
        
        
        self.scoreView.baseScrollView.contentOffset = CGPointMake(kScreenWidth, 0);
        [self switchButtonPoint];
        
        
        [UIView commitAnimations];
        
        
        [self NowHasNoPoint];
        
    }
    else
    {
        [self leftButton:self.scoreView.leftButton];
    }
}


#pragma mark - 拖动中执行
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    [UIView beginAnimations:nil context:nil];
    [UIView setAnimationDuration:0.5];
    [UIView setAnimationDelegate:self];
    
    int i = (self.scoreView.baseScrollView.contentOffset.x + kScreenWidth/2) / kScreenWidth;
    self.scoreView.pointImage.center = CGPointMake((i * kScreenWidth/2 + kScreenWidth/4), kScreenIp6(106));
    
    [UIView commitAnimations];
}

#pragma mark 拖拽后减速停止触发
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    [UIView beginAnimations:nil context:nil];
    [UIView setAnimationDuration:0.3];
    [UIView setAnimationDelegate:self];
    
    [self switchButtonPoint];
    
    [UIView commitAnimations];
    
    
    if (scrollView.contentOffset.x == kScreenWidth )
    {
        
        [self NowHasNoPoint];
        
    }
    
    
    
}



// 切换 Button 指向
- (void)switchButtonPoint
{
    if (self.scoreView.baseScrollView.contentOffset.x == 0)
    {
        self.scoreView.pointImage.center = CGPointMake(kScreenWidth/4, kScreenIp6(106));
        [self.scoreView.importantBtn setTintColor:[UIColor whiteColor]];
        [self.scoreView.totalBtn setTintColor:[UIColor colorWithWhite:0.900 alpha:1.000]];
    }
    else
    {
        self.scoreView.pointImage.center = CGPointMake(kScreenWidth*3/4, kScreenIp6(106));
        [self.scoreView.totalBtn setTintColor:[UIColor whiteColor]];
        [self.scoreView.importantBtn setTintColor:[UIColor colorWithWhite:0.900 alpha:1.000]];
    }
}

//
//// 切换 Button 指向
//- (void)switchButtonPoint
//{
//    if (self.scoreView.baseScrollView.contentOffset.x == 0)
//    {
//        self.scoreView.pointImage.center = CGPointMake(kScreenWidth/6, kScreenIp6(106));
//        [self.scoreView.recommendBtn setTintColor:[UIColor whiteColor]];
//        [self.scoreView.collectionBtn setTintColor:[UIColor colorWithWhite:0.900 alpha:1.000]];
//        [self.scoreView.videoBtn setTintColor:[UIColor colorWithWhite:0.900 alpha:1.000]];
//    }
//    else if (self.scoreView.baseScrollView.contentOffset.x == kScreenWidth)
//    {
//        self.scoreView.pointImage.center = CGPointMake(kScreenWidth/2, kScreenIp6(106));
//        [self.scoreView.collectionBtn setTintColor:[UIColor whiteColor]];
//        [self.scoreView.recommendBtn setTintColor:[UIColor colorWithWhite:0.900 alpha:1.000]];
//        [self.scoreView.videoBtn setTintColor:[UIColor colorWithWhite:0.900 alpha:1.000]];
//    }
//    else
//    {
//        self.scoreView.pointImage.center = CGPointMake(kScreenWidth*5/6, kScreenIp6(106));
//        [self.scoreView.videoBtn setTintColor:[UIColor whiteColor]];
//        [self.scoreView.collectionBtn setTintColor:[UIColor colorWithWhite:0.900 alpha:1.000]];
//        [self.scoreView.recommendBtn setTintColor:[UIColor colorWithWhite:0.900 alpha:1.000]];
//    }
//}



#pragma mark  关注按钮
-(void)pointAcion:(UIButton*)sender
{
    FirstFolltballViewCell * cell = (FirstFolltballViewCell *)[[sender superview] superview];
    NSIndexPath * path = [self.scoreView.leftTableView indexPathForCell:cell];
    
    FirstFootballModel *model = self.dateModelArray[path.section][path.row];
    
    
    model.isSelectPoint = !model.isSelectPoint;
    
    if( model.isSelectPoint)
    {
        
        [cell.attentionIcon  setBackgroundImage:[UIImage imageNamed:@"football_collected_small_1@2x"] forState:UIControlStateNormal];
        
        
        
        [self.pointBassketballModelArray addObject:model];
        
        //添加到数据库
        [[MyBassketballPointTool sharedMyBassketballPointTool]creatTable];
        [[MyBassketballPointTool sharedMyBassketballPointTool]addDataWithModel:model];
        //
        
        
        [self.scoreView.leftTableView reloadData];
        [self.scoreView.rightTableView reloadData];
        
        
    }
    else
    {
        [cell.attentionIcon  setBackgroundImage:[UIImage imageNamed:@"football_collect_small_1"] forState:UIControlStateNormal];
        
        for(int i= 0; i< self.pointBassketballModelArray.count;i++)
        {
            if([ [self.pointBassketballModelArray[i] matchId] isEqualToString:model.matchId])
                
            {
                
                [self.pointBassketballModelArray removeObjectAtIndex:i];
                
                
            }
            
            
        }
        
        //移除数据库
        [[MyBassketballPointTool sharedMyBassketballPointTool]creatTable];
        [[MyBassketballPointTool sharedMyBassketballPointTool]deleteModel:model];
        
        [self.scoreView.leftTableView reloadData];
        [self.scoreView.rightTableView reloadData];
        
    }
    
    //暂无关注
    [self NowHasNoPoint];
    
}
#pragma mark 关注取消
-(void)pointCancelAcion:(UIButton*)sender
{
    FirstFolltballViewCell * cell = (FirstFolltballViewCell *)[[sender superview] superview];
    NSIndexPath * path = [self.scoreView.leftTableView indexPathForCell:cell];
    
    FirstFootballModel *model = self.pointBassketballModelArray[path.row];
    
    
    model.isSelectPoint = !model.isSelectPoint;
    
    
    if(!model.isSelectPoint)
    {
        
        [self.pointBassketballModelArray removeObject:model];
        
        
        //删除关注
        [[MyBassketballPointTool sharedMyBassketballPointTool]creatTable];
        
        [[MyBassketballPointTool sharedMyBassketballPointTool]deleteModel:model];
        
        [self.scoreView.leftTableView reloadData];
        [self.scoreView.rightTableView reloadData];
        
    }
    
    //暂无关注
    [self NowHasNoPoint];
    
}

#pragma mark  暂无关注
-(void)NowHasNoPoint
{
    
    
    UIView *NoPointVIew = [[UIView alloc]initWithFrame:CGRectMake(kScreenWidth, 0, kScreenWidth, kScreenHeight)];
    NoPointVIew.backgroundColor = [UIColor whiteColor];
    
    
    
    UIImageView *pointPicture = [[UIImageView alloc]initWithFrame:CGRectMake(kScreenWidth/2 - kScreenIp6(100), kScreenIp6(100), kScreenIp6(200), kScreenIp6(200))];
    pointPicture.image = [UIImage imageNamed:@"Empty_Hint_Fav@2x"];
    [NoPointVIew addSubview:pointPicture];
    
    
    
    if(self.pointBassketballModelArray.count == 0)
    {
    
        [self.scoreView.baseScrollView  addSubview:NoPointVIew];

        
        
        
    }
    else
    {
    
    [NoPointVIew removeFromSuperview];
     [self.scoreView.baseScrollView   addSubview:self.scoreView.rightTableView];
        

    }
    
    
    
    
    
    
}



#pragma mark - 隐藏和显示状态栏
- (void)viewWillAppear:(BOOL)animated
{
    self.navigationController.navigationBarHidden = YES;
    
    
    [[MyBassketballPointTool sharedMyBassketballPointTool]creatTable];
    
    self.pointBassketballModelArray =[[[MyBassketballPointTool sharedMyBassketballPointTool]getModels]mutableCopy];
}


- (void)viewWillDisappear:(BOOL)animated
{
    self.navigationController.navigationBarHidden = NO;
}




@end
